<?php
/**
 * (jobs) 备货国家品类/sku预估
 */
namespace App\Jobs\Stocking;

use App\Models\Basics\SysArea;
use App\Models\SsBasic\CountrySkuEstimate;
use App\Models\SsBasic\CountryStockingSku;
use App\Models\SsBasic\CountryStockingSkuRatio;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class CountryEstimateJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $estimate;

    /**
     * Estimate constructor.
     * @param $estimate
     */
    public function __construct($estimate)
    {
        $this->estimate = $estimate;
    }

    /**
     * Execute the job.
     * @return void
     */
    public function handle()
    {
        $this->exeFillData($this->estimate);
    }

    /**
     * 1.更新s_country_classify_estimate
     * 1.1.根据比例将品类分配到对应SKU的预估（s_country_sku_estimate）
     * 1.2.更新EU国家备货产品的预估（s_country_stocking_sku）
     * @param $estimate
     * @return void
     */
    private function exeFillData($estimate){

        $areaId = SysArea::AREA_EU; //EU站点

        //根据比例，将EU国家品类预估分解到EU国家sku预估，同步更新EU国家sku备货表里的预估
        //更新,EU国家sku预估、EU国家备货sku及相关信息
        foreach ($estimate as $countryClassifyEstimate){
            $countryId = $countryClassifyEstimate->country_id ?? 0;
            $classifyId = $countryClassifyEstimate->classify_id ?? 0;

            //备货sku(EU国家)
            $countryStockingSkus = CountryStockingSku::query()
                ->where('country_id',$countryId)
                ->where('classify_id',$classifyId)
                ->get();
            if ($countryStockingSkus) {
                foreach ($countryStockingSkus as $stockingSku){
                    list($euCountryId,$pSkuId) = explode('_',$stockingSku->area_psku);
                    $countryPsku = $euCountryId.'_'.$pSkuId; //EU国家+SKU

                    //sku销售比例(EU国家)
                    $ratio = CountryStockingSkuRatio::getRatio($countryPsku);

                    //sku预估(EU国家)
                    $skuEstimate = CountrySkuEstimate::query()->firstOrNew(['area_psku' => $countryPsku]);
                    for($i = 0;$i < CountrySkuEstimate::ESTIMATE_NUM; $i++){
                        $num = $countryClassifyEstimate->{'estimate_num'.$i} ?? 0; //赋值
                        $estimateNum = intval(round($num * $ratio));
                        $stockingSku->{'estimate_num'.$i} = $estimateNum;
                        $skuEstimate->{'estimate_num'.$i} = $estimateNum;
                    }
                    $skuEstimate->area_id = $areaId;
                    $skuEstimate->country_id = $euCountryId;
                    $skuEstimate->psku_id = $pSkuId;
                    $skuEstimate->classify_id = $classifyId;
                    $skuEstimate->save();

                    $stockingSku->area_id = $areaId;
                    $stockingSku->country_id = $euCountryId;
                    $stockingSku->psku_id = $pSkuId;
                    $stockingSku->classify_id = $classifyId;
                    $stockingSku->save();
                    $stockingSku->over_stock = CountryStockingSku::getOverStock($stockingSku);
                    $stockingSku->over_stock_value = CountryStockingSku::getOverStockValue($stockingSku->over_stock,$stockingSku->unit_price);
                    $stockingSku->save();
                }
            }
        }
        sleep(1);
    }

}

